package com.reguang.result;

import java.io.Serializable;
import java.util.Date;

import com.reguang.enums.ResultCode;
import com.reguang.utils.RequestContextHolderUtil;

public class ErrorResult implements Serializable {

	/** serialVersionUID */
	private static final long serialVersionUID = 4527051068092349776L;

	/**
	 * 我们系统内部自定义的返回值编码，{@link ResultCode} 它是对错误更加详细的编码
	 * 
	 * 备注：spring boot默认返回异常时，该字段为null
	 */
	private Integer code;

	/**
	 * 异常堆栈的精简信息
	 * 
	 */
	private String message;

	/**
	 * 调用接口路径
	 */
	private String path;

	/**
	 * 时间戳
	 */
	private Date timestamp;

	public ErrorResult() {
		super();
	}

	public static ErrorResult failure(ResultCode resultCode, String message) {
		ErrorResult result = new ErrorResult();
		result.setCode(resultCode.code());
		result.setMessage(message);
		result.setPath(RequestContextHolderUtil.getRequest().getRequestURI());
		result.setTimestamp(new Date());
		return result;
	}

	public static ErrorResult failure(ResultCode resultCode) {
		ErrorResult result = new ErrorResult();
		result.setCode(resultCode.code());
		result.setMessage(resultCode.message());
		result.setPath(RequestContextHolderUtil.getRequest().getRequestURI());
		result.setTimestamp(new Date());
		return result;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public Date getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(Date timestamp) {
		this.timestamp = timestamp;
	}

}
